import { Controller, Post, Get, UseGuards, Body, Req } from '@nestjs/common';
import { CheckService } from './check.service';
import { Request } from 'express';
import { ApiBody, ApiOperation, ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { CheckDTO } from 'src/dto/check/check.dto';
import { User } from 'src/entities/user.entity';
import { Public } from 'src/common/decorators/public.decorator';

@ApiTags('check')
// swagger请求头携带token发生请求
@ApiBearerAuth()
@Controller('check')
export class CheckController {
  constructor(private readonly checkService: CheckService,
  ) { }
  @ApiOperation({ summary: "添加个人考勤数据" })
  @Post("addCheck")
  async register(@Req() req: Request, @Body() check: CheckDTO): Promise<any> {
    if (check.userId) (req.user as User).id = check.userId
    check.user = req.user as User
    console.log(' check.user ', check);

    await this.checkService.addCheck(check);
    return "添加成功"
  }

  @ApiOperation({ summary: "更新个人考勤数据" })
  @Post("updateCheck")
  async updateCheck(@Req() req: Request, @Body() check: CheckDTO): Promise<any> {
    console.log(' check.user ', check);
    await this.checkService.updateCheck(check.id, check);
    return "更新成功"
  }

  @ApiOperation({ summary: "查找个人考勤数据（按月）" })
  @Post("findCheck")
  async findCheck(@Req() req: Request, @Body() check: CheckDTO): Promise<any> {
    let id = check.id || (req.user as User).id;
    console.log(' check.user ', id);

    const res = await this.checkService.findCheck(id, check);
    return res
  }

  @ApiOperation({ summary: "查找全部人考勤数据（按月）" })
  @Post("findCheckAll")
  async findCheckAll(@Req() req: Request, @Body() check: CheckDTO): Promise<any> {
    const res = await this.checkService.findCheckAll(check);
    console.log('res全部考勤数据', res);

    return res
  }
}
